define(function (require, exports, module) {
	var EditGrid = require("editgrid");
	var store = require("store");
	var Button = require("button");
	var Dialog = require("dialog");
	var activeUser = store.get("user");
	
    var instance = {
        initComponent: function (params, widget) {
        	var editgrid = new EditGrid({
        		parentNode: ".J-editgrid",
        		model: {
        			id:"editgrid",
        			isInitPageBar: false,
        			allowAdd:false,
        			columns: [{
						name:"nonMemberRegister.signUpRegister.personalInfo.idNumber",
						label:"身份证号"
					},{
						name:"nonMemberRegister.signUpRegister.personalInfo.name",
						label:"姓名"
					},{
						name:"nonMemberRegister.signUpRegister.personalInfo.sex",
						label:"性别",
                        format:function(value,row){
                        	if(typeof value == "string"){
                        		if(value == "Male"){
                        			return "男";
                        		}else{
                        			return "女";
                        		}
                        	}else{
                        		if(value.key == "Male"){
                        			return "男";
                        		}else{
                        			return "女";
                        		}
                        	}
                        }
					},{
                    	name : "nonMemberRegister.signUpRegister.personalInfo.mobilePhone",
                    	label : "手机号(可编辑)",
                    	editor: {
                            type: "text",
                            onEdit:function(editor,rowIndex,rowData){
								editor.setValue(rowData.nonMemberRegister.signUpRegister.personalInfo.mobilePhone);
							},
                            onChange: function(plugin, rowIndex, rowData) {
                            	var value = plugin.getValue();
                            	var reg = /^1(3|4|5|7|8)\d{9}$/;
                            	if (reg.test(value)) {
                            		rowData.nonMemberRegister.signUpRegister.personalInfo.mobilePhone = value;
                                    editgrid.update(rowIndex, rowData);
		                        }else {
		                        	Dialog.alert({
				                		title:"提示",
				                		content:"请输入正确的手机号！"
				                	});
		                        }
                            	
                            }
                        }
                    },{
						name:"nonMemberRegister.signUpRegister.travelContract.memberCertificates",
						label:"会员证号(可编辑)",
						format : function(value, row) {
							var travelContract = row.nonMemberRegister.signUpRegister.travelContract;
							widget.set("travelContract",row.nonMemberRegister.signUpRegister.travelContract);
							if(travelContract==null){
								return "";
							}else{
								return travelContract.member.personalInfo.idNumber
								+ "-"
								+ travelContract.member.personalInfo.name
								+ "-"
								+ travelContract.productRule.product.name
								+ "-"
								+ travelContract.memberCertificates;
							}
	        			},
						editor:{
							type : "autocomplete",
							url : "api/signupregister/querycertificates",
							queryParamName : "s",
							keyField: "pkTravelContract",
							format : function(data) {
								
								if(typeof data == "string"){
									return data;
								}else if(data && typeof data == "object"){
									var obj = {travelContract : data};
									return obj.travelContract.member.personalInfo.idNumber
									+ "-"
									+ obj.travelContract.member.personalInfo.name
									+ "-"
									+ obj.travelContract.productRule.product.name
									+ "-"
									+ obj.travelContract.memberCertificates;
								}
		        			},
							params: function(str){
								return {
									"pkServicePoint":activeUser.servicePoint.pkServicePoint,
		                       		"fetchProperties":"pkTravelContract,memberCertificates,version,member.personalInfo.name," +
		                       				"member.personalInfo.idNumber,productRule.product.name,productRule.product.pkProduct," +
		                       				"productRule.product.version"
								};
							},
							onChange:function(plugin,index,rowData){
								rowData.nonMemberRegister.signUpRegister.travelContract = plugin.getData();
								editgrid.update(index,rowData);
							}
						},
					},{
						name:"roomRegister.room.roomType.name",
						label:"房型"
					},{
						name:"roomRegister.room.code",
						label:"房号"
					},{
						name:"checkIn",
						label:"入住登记",
						editor:{
							type:"select",
							options:[{
								key:"true",
								value:"是"
							},{
								key:"false",
								value:"否"
							}],
							onEdit:function(editor,rowIndex,rowData){
								editor.setValue(rowData.checkIn);
							},
							onChange:function(plugin,index,rowData){
								rowData.checkIn = plugin.getData(plugin.getValue());
								editgrid.update(index,rowData);
							}
						},
						format:function(value,row){
							if(typeof value == "object"){
								return value.value;
							}else{
								if(value == true || value == "true"){
									return "是";
								}else{
									return "否";
								}
							}
						}
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"reset",
							text: "重置",
							show:function(value,row){
								var sourceType = widget.get("form").getValue("sourceType");
					        	if(sourceType == "DropIn"){
					        		return false;
					        	}else{
					        		return true;
					        	}
							},
							handler:function(index,data,rowEle){
								data.roomRegister.room = null;
								editgrid.update(index,data);
							}
						},{
							id:"delete",
							text: "删除",
							show:function(value,row){
								var sourceType = widget.get("form").getValue("sourceType");
					        	if(sourceType == "DropIn"){
					        		return true;
					        	}else{
					        		return false;
					        	}
							},
							handler:function(index,data,rowEle){
								editgrid.remove(index);
							}
						}]
					}]
        		}
        	});
        	widget.set("editgrid",editgrid);
        	var selectRoom = new Button({
				parentNode: ".J-editgrid .J-grid-title",
				model: {
					text: "选择房间",
					handler: function(){
						widget.toRoomStatus();
					}
				}
			});
			widget.set("selectRoom",selectRoom);
        }
    };
    module.exports = instance;
});